<?php

namespace app\util;
use app\api\service\log\UserOperateLogService;
use app\service\OperateLogService;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class UserUtil
{
    public static function getLoginUserId()
    {
        $userInfo = self::getUserLoginInfo();
        if(!empty($userInfo) && isset($userInfo['user_id'])){
            return $userInfo['user_id'];
        }else{
            return 0;
        }
    }

    public static function validatePass($pass, $dbPass)
    {
        $sign = self::encryptPass($pass);
        $postSign = substr($sign, 0, strlen($sign) - 6);
        $dbSign = substr($dbPass, 0, strlen($dbPass) - 6);
//        echo $postSign."---".$dbSign;
        if ($postSign === $dbSign) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Desc: 密码加密
     * @param $password
     * @return string
     * Datetime: 2024/12/13 11:14 by zdb
     */
    public static function encryptPass($password)
    {
        $prefix = "yj2025";
        $arrHaystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $arrRandIndex = array_rand($arrHaystack, 6);
        $randStr = '';
        foreach ($arrRandIndex as $index) {
            $randStr .= $arrHaystack[$index];
        }
        return md5($prefix . $password) . $randStr;
    }

    public static function encodeUserInfo($data)
    {
        return JWT::encode($data, config('app.jwt_encrypt_key'), 'HS256');
    }

    public static function decodeUserInfo($token)
    {
        $key = new Key(config('app.jwt_encrypt_key'), 'HS256');
        return JWT::decode($token, $key);
    }

    public static function getUserLoginInfo(){
        $jsonData = isset($_SESSION['user_login_info']) ? $_SESSION['user_login_info']:'[]';
        return json_decode($jsonData, true);
    }

    /**
     * 新增用户操作日志
     * @param $pageName 页面名称
     * @param $operationFunc 操作方法
     * @param $actionType 行为类型
     * @param $logData 日志信息
     * @return int|string
     */
    public static function addOperateLog($pageName, $operationFunc, $actionType, $logData)
    {
        $loginUserInfo = self::getUserLoginInfo();
        $data = [
            'user_code'       => $loginUserInfo['user_code'],
            'action_type'   => $actionType,
            'page_name'        => $pageName,
            'operation'        => $operationFunc,
            'subject'             => $logData['subject'] ?? '',
            'remark'              => $logData['remark'] ?? '',
            'description_en'      => $logData['description_en'] ?? "",
            'description_cn'      => $logData['description_cn'] ?? "",
        ];
        $serviceLog = new UserOperateLogService();
        return $serviceLog->createRow($data);
    }

    public static function addOperateHistory($moduleName, $subModuleName, $actionType, $logData)
    {
//        $loginUserInfo = self::getUserLoginInfo();
        $data = [
            'user_id'       => 1, //$loginUserInfo['user_id'],
            'user_name'     => 'test',//$loginUserInfo['real_name'],
            'action_type'   => $actionType,
            'module'        => $moduleName,
            'sub_module'        => $subModuleName,
            'subject'             => $logData['subject'] ?? '',
            'remark'              => $logData['remark'] ?? '',
            'description_en'      => $logData['description_en'] ?? "",
            'description_cn'      => $logData['description_cn'] ?? "",
        ];
        $serviceLog = new UserOperateLogService();
        return $serviceLog->addUserOperateLog($data);
    }

    public static function makeUserCode()
    {
        $prefix = "yj";
        $randNum = 5;
        $isUpperCase = false;
        return CommonUtil::makeUniqueCode($prefix, $randNum, $isUpperCase);
    }
}